package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import com.google.gson.Gson;

import model.Categoriearret;
import model.Descriptionarret;
import model.Ligne;
import model.Operateur;
import model.Stock;



/**
 * Servlet implementation cl ass ActionServlet
 */

public class ActionServlet extends HttpServlet {
	
	Operation op ;
	HttpSession session;
	@Override
	public void init() throws ServletException {
		
		op = new Operation();
	}
	
	
	
	private static final long serialVersionUID = 1L;
	//private static final String UserRecord = null;
	//Déclarer des variables
	private static String Stock = "/incident.jsp";
	


	public ActionServlet(){
		super();
		//UserDao dao = new UserDao();
	}
	//declaration de la methode doGet
	//La méthode doGet permet de récupérer les informations
	
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		//une variable utilisé a la fin pour la redirection
		String redirect = Stock;
		
		//On récupére les valeurs des paramètres
		//On récupère la valeur du paramètre ID
		String uId = req.getParameter("id");
		
		/*On récupère la valeur du paramètre action qui prend les valeurs
		 * insert, edit, editform, listUser ou delete les cas que nous allons
		 * traité pa la suite
		 */
		String action = req.getParameter("action");
		//Actions
		if(action.equalsIgnoreCase("Ajouter")){
			Stock s = new Stock();
			
			s.setDate(req.getParameter("Date"));
			s.setCategorie(req.getParameter("NomCat"));
			s.setArret(req.getParameter("Arret"));
			s.setLigne(req.getParameter("Ligne"));
			s.setDuree(req.getParameter("Duree"));
			
			op.ajoutSt(s);
			UhtBeans ub = new UhtBeans();
			ub.setListSt(op.allStock());
			
			req.setAttribute("iddd", req.getParameter("NomCat"));
			System.out.println("ID DU CATEGORIE EST : "+req.getParameter("NomCat"));
			req.setAttribute("stock", ub);
			
			req.getRequestDispatcher("Table.jsp").forward(req, resp);
		}
		if (action.equalsIgnoreCase("Modifier")){
			Stock s = op.stockId(req.getParameter("Date"));
			
			s.setDate(req.getParameter("Date"));
			s.setCategorie(req.getParameter("NomCat"));
			s.setArret(req.getParameter("Arret"));
			s.setLigne(req.getParameter("Ligne"));
			s.setDuree(req.getParameter("Duree"));
			
			op.updateStock(s);
			UhtBeans ub = new UhtBeans();
			ub.setListSt(op.allStock());			
			req.setAttribute("stock", ub);
			
			req.getRequestDispatcher("Table.jsp").forward(req, resp);
		}
		
		if (action.equalsIgnoreCase("Supprimer")){
			Stock s = op.stockId(req.getParameter("Date"));
			
			op.deleteStock(s);
			UhtBeans ub = new UhtBeans();
			ub.setListSt(op.allStock());
			
			req.setAttribute("stock", ub);
			req.getRequestDispatcher("Table.jsp").forward(req, resp);
		}
		
		 
		if(action.equalsIgnoreCase("Rechercher")){
			UhtBeans ub = new UhtBeans();
			ub.setListSt(op.getStockById(req.getParameter("Date")));
			req.setAttribute("stock", ub);
			req.getRequestDispatcher("Table.jsp").forward(req, resp);
		}
		 
		 if (action.equalsIgnoreCase("Retourner")){
			 UhtBeans pb = new UhtBeans();
			 pb.setListSt(op.allStock());
			 
			 req.setAttribute("stock", pb);
			 req.getRequestDispatcher("Table.jsp").forward(req, resp);
		 }
		 
		//**********Actions pour les operateurs
			if(action.equalsIgnoreCase("AjouterOp")){
				Operateur oper = new Operateur();
				
				oper.setIdoperateur(req.getParameter("IdOp"));
				oper.setNomoperateur(req.getParameter("NomOp"));
				oper.setLoginop(req.getParameter("LoginOp"));
				oper.setPassop(req.getParameter("PassOp"));
				oper.setIsadmin(req.getParameter("admin"));
						
				op.ajoutOperateur(oper);
				UhtBeans ub = new UhtBeans();
				ub.setListOp(op.allOperateur());
				req.setAttribute("operateur", ub);
	
				req.getRequestDispatcher("TableOp.jsp").forward(req, resp);
				}
			
			if (action.equalsIgnoreCase("ModifierOp"))
			{
				Operateur oper = op.OperateurId(req.getParameter("IdOp"));
				
				oper.setIdoperateur(req.getParameter("IdOp"));
				oper.setNomoperateur(req.getParameter("NomOp"));
				oper.setLoginop(req.getParameter("LoginOp"));
				oper.setPassop(req.getParameter("PassOp"));
				oper.setIsadmin(req.getParameter("admin"));
				
				op.updateOperateur(oper);
				UhtBeans ub = new UhtBeans();
				ub.setListOp(op.allOperateur());
				req.setAttribute("operateur", ub);
								
				req.getRequestDispatcher("TableOp.jsp").forward(req, resp);
			}
			 if (action.equalsIgnoreCase("SupprimerOp"))
				{
				 
				 Operateur oper = op.OperateurId(req.getParameter("IdOp"));
				 
				 op.deleteOperateur(oper);
				 UhtBeans ub = new UhtBeans();
				 ub.setListOp(op.allOperateur());
				 req.setAttribute("operateur", ub);
				 
				 req.getRequestDispatcher("TableOp.jsp").forward(req, resp);
				 
				}
			 
			 if (action.equalsIgnoreCase("RechercherOp"))
				{
				 UhtBeans ub = new UhtBeans();
					ub.setListOp(op.getOperateurById(req.getParameter("IdOp")));
					req.setAttribute("operateur", ub);
					req.getRequestDispatcher("TableOp.jsp").forward(req, resp);
				}
			 if (action.equalsIgnoreCase("RetournerOp"))
				{
				 
				 UhtBeans pb = new UhtBeans();
				 pb.setListOp(op.allOperateur());
									
				 req.setAttribute("operateur", pb);
				 req.getRequestDispatcher("TableOp.jsp").forward(req, resp);
						
				}
			 if (action.equalsIgnoreCase("Appel") )
				{
				 Ligne li = new Ligne();

				 li.setIdligne(req.getParameter(""));
				 li.setNomligne(req.getParameter(""));

					op.updateLigne(li);
					UhtBeans pb = new UhtBeans();
					pb.setListeLi(op.allLigne());
					req.setAttribute("incident", pb);
			
					req.getRequestDispatcher("Table.jsp").forward(req, resp);
				}
			 
			 // Page d'authentification
			 if (action.equalsIgnoreCase("Connecter") )
				{
				 Operateur opr = new Operateur();
					opr.setLoginop(req.getParameter("login"));
					opr.setPassop(req.getParameter("pass"));
					
					
					UhtBeans pb = new UhtBeans();
					pb.setLogin(true);
					req.setAttribute("logg", pb);
					req.getRequestDispatcher("tab.jsp").forward(req, resp);
				}


		
		
		//***********Actions pour les Lignes
			if(action.equalsIgnoreCase("userByID")){
				Operation op = new Operation();
				try{
					Gson json = new Gson();
					PrintWriter pw = resp.getWriter();
					pw.write(json.toJson(op.getArretByCat(req.getParameter("id").toString())));
				}catch (IOException e){
					
					e.printStackTrace();
				}
			}

			
			if(action.equalsIgnoreCase("AjouterLi")){
				Ligne li = new Ligne();
				
				li.setIdligne(req.getParameter("IdLi"));
				li.setNomligne(req.getParameter("NomLi"));
						
				op.ajoutLi(li);
				UhtBeans ub = new UhtBeans();
				ub.setListeLi(op.allLigne());
				req.setAttribute("ligne", ub);
	
				req.getRequestDispatcher("TableLi.jsp").forward(req, resp);
				}
			
			if (action.equalsIgnoreCase("ModifierLi"))
			{
				Ligne li = op.ligneId(req.getParameter("IdLi"));
				
				li.setIdligne(req.getParameter("IdLi"));
				li.setNomligne(req.getParameter("NomLi"));
				
				op.updateLigne(li);
				UhtBeans ub = new UhtBeans();
				ub.setListeLi(op.allLigne());
				req.setAttribute("ligne", ub);
								
				req.getRequestDispatcher("TableLi.jsp").forward(req, resp);
			}
			 if (action.equalsIgnoreCase("SupprimerLi"))
				{
				 
				 Ligne li = op.ligneId(req.getParameter("IdLi"));
				 
				 op.deleteLi(li);
				 UhtBeans ub = new UhtBeans();
				 ub.setListeLi(op.allLigne());
				 req.setAttribute("ligne", ub);
				 
				 req.getRequestDispatcher("TableLi.jsp").forward(req, resp);
				 
				}
			 
			 
			 if (action.equalsIgnoreCase("RechercherLi") )
				{
				  UhtBeans ub = new UhtBeans();
					ub.setListeLi(op.getLigneById(req.getParameter("IdLi")));
					req.setAttribute("ligne", ub);
					req.getRequestDispatcher("TableLi.jsp").forward(req, resp);
				}
			 if (action.equalsIgnoreCase("RetournerLi") )
				{
				 
				 UhtBeans pb = new UhtBeans();
				 pb.setListeLi(op.allLigne());
									
				 req.setAttribute("ligne", pb);
				 req.getRequestDispatcher("TableLi.jsp").forward(req, resp);
						
				}
			 if (action.equalsIgnoreCase("Appel") )
				{
				 Ligne li = new Ligne();

				 li.setIdligne(req.getParameter(""));
				 li.setNomligne(req.getParameter(""));

					op.updateLigne(li);
					UhtBeans pb = new UhtBeans();
					pb.setListeLi(op.allLigne());
					req.setAttribute("incident", pb);
			
					req.getRequestDispatcher("Table.jsp").forward(req, resp);
				}
			 
			 // Page d'authentification
			 if (action.equalsIgnoreCase("Connecter") )
				{
	
				}
			 
			 //Actions sur les Catégories
			 
			 if(action.equalsIgnoreCase("AjouterCat")){
				 
				 Categoriearret cat = new Categoriearret();
				 
				 cat.setIdfam(req.getParameter("IdCat"));
				 cat.setNomcat(req.getParameter("NomCat"));
				 
				 op.ajoutCat(cat);
				  
				 UhtBeans ub = new UhtBeans();
				 ub.setListeCat(op.allCategorie());
				 req.setAttribute("categorie", ub); 
				 
				 req.getRequestDispatcher("TableCat.jsp").forward(req, resp);
			 }
			 if(action.equalsIgnoreCase("ModifierCat")){
				 Categoriearret cat = op.categorieId(req.getParameter("IdCat"));
				 
				 cat.setIdfam(req.getParameter("IdCat"));
				 cat.setNomcat(req.getParameter("NomCat"));
				 
				 op.updateCat(cat);
				 UhtBeans ub = new UhtBeans();
				 ub.setListeCat(op.allCategorie());
				 req.setAttribute("categorie", ub);
				 
				 req.getRequestDispatcher("TableCat.jsp").forward(req, resp);
			 }
			 
			 if(action.equalsIgnoreCase("SupprimerCat")){
				 Categoriearret cat = op.categorieId(req.getParameter("IdCat"));
				 
				 op.deleteCat(cat);
				 UhtBeans ub = new UhtBeans();
				 ub.setListeCat(op.allCategorie());
				 req.setAttribute("categorie", ub);
				 
				 req.getRequestDispatcher("TableCat.jsp").forward(req, resp);
			 }
			 
			 if (action.equalsIgnoreCase("RechercherCat")){
				 UhtBeans ub = new UhtBeans();
				 ub.setListeCat(op.getCategorieById(req.getParameter("IdCat")));
				 req.setAttribute("categorie", ub);
				 
				 req.getRequestDispatcher("TableCat.jsp").forward(req, resp);
			 }
			 
			 if (action.equalsIgnoreCase("RetournerCat")){
				 
				 UhtBeans pb = new UhtBeans();
					pb.setListeCat(op.allCategorie());
									
				 req.setAttribute("categorie", pb);
				 req.getRequestDispatcher("TableCat.jsp").forward(req, resp);
						
					
					
				}
			
				
				//Actions sur les Arrêts
				 
				 if(action.equalsIgnoreCase("AjouterArr")){
					 
					 Descriptionarret arr = new Descriptionarret();
					 
					 arr.setIdarret(req.getParameter("IdArr"));
					 arr.setNomarret(req.getParameter("NomArr"));
					 arr.setCategorie(req.getParameter("NomCat"));
					 
					 op.ajoutArr(arr);
					  
					 UhtBeans ub = new UhtBeans();
					 ub.setListeArr(op.allArret());
					 req.setAttribute("arret", ub); 
					 
					 req.getRequestDispatcher("TableArr.jsp").forward(req, resp);
				 }
				 if(action.equalsIgnoreCase("ModifierArr")){
					 Descriptionarret arr = op.ArretId(req.getParameter("IdArr"));
					 
					 arr.setIdarret(req.getParameter("IdArr"));
					 arr.setNomarret(req.getParameter("NomArr"));
					 arr.setCategorie(req.getParameter("NomCat"));
					 
					 op.updateArr(arr);
					 UhtBeans ub = new UhtBeans();
					 ub.setListeArr(op.allArret());
					 req.setAttribute("arret", ub);
					 
					 req.getRequestDispatcher("TableArr.jsp").forward(req, resp);
				 }
				 
				 if(action.equalsIgnoreCase("SupprimerArr")){
					 Descriptionarret arr = op.ArretId(req.getParameter("IdArr"));
					 
					 op.deleteArr(arr);
					 UhtBeans ub = new UhtBeans();
					 ub.setListeArr(op.allArret());
					 req.setAttribute("arret", ub);
					 
					 req.getRequestDispatcher("TableArr.jsp").forward(req, resp);
				 }
				 
				 if (action.equalsIgnoreCase("RechercherArr")){
					 UhtBeans ub = new UhtBeans();
					 ub.setListeArr(op.getArretById(req.getParameter("IdArr")));
					 req.setAttribute("arret", ub);
					 
					 req.getRequestDispatcher("TableArr.jsp").forward(req, resp);
				 }
				 
			
					 if (action.equalsIgnoreCase("RetournerArr") )
						{
						 
						 UhtBeans pb = new UhtBeans();
							pb.setListeArr(op.allArret());
											
						 req.setAttribute("arret", pb);
						 req.getRequestDispatcher("TableArr.jsp").forward(req, resp);
								
							
							
						}


				
							
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
        
		 session = req.getSession();
		
	
		 if (req.getParameter("actionConnecter")!=null)
			{
			 Operateur opr = new Operateur();
			 opr.setLoginop(req.getParameter("login"));
			 opr.setPassop(req.getParameter("pass"));
		
		if(op.opera(opr) != null){
			UhtBeans pb = new UhtBeans();
			pb.setOperateur(op.opera(opr));
			
			session.setAttribute("oppp", pb.getOperateur());
			
		    resp.sendRedirect("Index.jsp");
			
		
		
		}
		else {
			
			JOptionPane.showMessageDialog(null,"Login ou Mot de pass Sont Incorrectes");
			resp.sendRedirect("Connexion.jsp");
		}}
		 if (req.getParameter("Adminpanel4")!=null)
			{
			 session.invalidate();
			 resp.sendRedirect("Connexion.jsp");
			}
		
	}

}
